﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CapstoneProjectAEReader.Business.Others;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using CapstoneProjectAEReader.Entities.Models;

namespace CapstoneProjectAEReader.GUIs.TranslationGUI
{
    /// <summary>
    /// Interaction logic for GoogleTranslate.xaml
    /// </summary>
    public partial class Translation : Window
    {
        #region Attributes

        private DAO _dao;
        private Ebook _ebook;
        private string _selectedText;
        private int _currentPage;
        private const string SelectedText = "selectedText";
        private const string From = "from";
        private const string To = "to";
        private const string TranslatedText = "translatedText";
        private const string AddNote = "addNote";
        private const string Translate = "translate";
        private const string Cancel = "cancelButton";
        private const string Vietnamese = "vietnamese";
        private const string English = "english";
        private const string Japanese = "japanese";
        private const string EbookNotExist = "ebookNotExist";

        #endregion

        public Translation(DTO dto, Ebook ebook)
        {
            _ebook = ebook;
            _dao = new DAO(dto);
            InitializeComponent();
            ReloadFormLanguage();
        }

        public Translation(string strSelectedText, DTO dto, Ebook ebook, int currentPage)
        {
            _currentPage = currentPage;
            _ebook = ebook;
            _dao = new DAO(dto);
            InitializeComponent();
            ReloadFormLanguage();
            _selectedText = strSelectedText;
            txtSelectedText.Text = strSelectedText;
        }

        public void ReloadFormLanguage()
        {
            lbTranslateTitle.Content = ResourceReader.GetResourceContent(SelectedText);
            lbFrom.Content = ResourceReader.GetResourceContent(From);
            lbTo.Content = ResourceReader.GetResourceContent(To);
            lbResultTitle.Content = ResourceReader.GetResourceContent(TranslatedText);
            lbTranslation.Content = ResourceReader.GetResourceContent(Translate);
            lbAddNote.Content = ResourceReader.GetResourceContent(AddNote);
            lbCancel.Content = ResourceReader.GetResourceContent(Cancel);
            //ComboBoxItem vns = new ComboBoxItem();
            //vns.Content = ResourceReader.GetResourceContent(Vietnamese);
            //ComboBoxItem eng = new ComboBoxItem();
            //eng.Content = ResourceReader.GetResourceContent(English);
            //ComboBoxItem jpn = new ComboBoxItem();
            //jpn.Content = ResourceReader.GetResourceContent(Japanese);
            //List<ComboBoxItem> dataSource = new List<ComboBoxItem>();
            //dataSource.Add(vns);
            //dataSource.Add(eng);
            //dataSource.Add(jpn);
            //List<ComboBoxItem> dataSourceDes = new List<ComboBoxItem>();
            //dataSourceDes.Add(vns);
            //dataSourceDes.Add(eng);
            //dataSourceDes.Add(jpn);
            //cbSourceLanguage.ItemsSource = dataSource;
            //cbSourceLanguage.SelectedIndex = 0;
            //cbTargetLanguage.ItemsSource = dataSourceDes;
            //cbTargetLanguage.SelectedIndex = 1;
        }

        private void btTranslate_Click(object sender, RoutedEventArgs e)
        {
            Translator t = new Translator();
            t.SourceLanguage = cbSourceLanguage.SelectionBoxItem.ToString();
            t.TargetLanguage = cbTargetLanguage.SelectionBoxItem.ToString();
            if (!t.SourceLanguage.Equals(t.TargetLanguage))
            {
                t.SourceText = txtSelectedText.Text;
                t.Translate();
                txtTranslatedText.Text = t.Translation;
            }
        }

        private void HandleBtClose(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void HandleBtAddNote(object sender, RoutedEventArgs e)
        {
            string translationResult = String.Format(Constants.TranslationResult,
                                                     cbTargetLanguage.SelectionBoxItem,
                                                     txtTranslatedText.Text.Trim());
            if (_dao.CheckEbookExist(_ebook))
            {
                if (_dao.CheckNoteExist(_selectedText))
                {
                    Note tempNote = _dao.GetNote(_selectedText);
                    NoteDetail tempNoteDetail = new NoteDetail();
                    if (_dao.CheckNoteDetailExist(tempNote.NoteId, _ebook.Id))
                    {
                        tempNoteDetail = _dao.GetNoteDetail(tempNote.NoteId, _ebook.Id);
                        tempNoteDetail.Description += Constants.CommonCharacters.NextLine + translationResult;
                        _dao.UpdateNoteDetail(tempNoteDetail);
                    }
                    else
                    {
                        tempNoteDetail.Bookid = _ebook.Id;
                        tempNoteDetail.PageNumber = _currentPage;
                        tempNoteDetail.Noteid = tempNote.NoteId;
                        tempNoteDetail.Createdate = DateTime.Now.ToString(Constants.FullDateTimeFormatToSecond);
                        tempNoteDetail.Description = translationResult;
                        _dao.InsertNoteDetail(tempNoteDetail);
                    }
                }
                else
                {
                    Note tempNote = new Note();
                    tempNote.NoteContent = _selectedText;
                    tempNote.NoteId = _dao.InsertNote(tempNote);
                    NoteDetail tempNoteDetail = new NoteDetail();
                    tempNoteDetail.Bookid = _ebook.Id;
                    tempNoteDetail.PageNumber = _currentPage;
                    tempNoteDetail.Noteid = tempNote.NoteId;
                    tempNoteDetail.Createdate = DateTime.Now.ToString(Constants.FullDateTimeFormatToSecond);
                    tempNoteDetail.Description = translationResult;
                    _dao.InsertNoteDetail(tempNoteDetail);
                }
                Close();
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(EbookNotExist));
            }
        }
    }
}